package com.ruoyi.common.service.business.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.pagination.Paging;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.pagination.PageInfo;
import com.ruoyi.common.domain.business.ProductSku;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.param.ProductParam;
import com.ruoyi.common.service.business.ProductSkuService;
import com.ruoyi.common.utils.satoken.StpWxUtils;
import com.ruoyi.common.vo.business.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.mapper.business.ProductMapper;
import com.ruoyi.common.domain.business.Product;
import com.ruoyi.common.service.business.ProductService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品Service业务层处理
 *
 * @author Song
 * @date 2024-10-29
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuService skuService;

    /**
     * 查询商品列表
     *
     * @param product 商品
     * @return 商品
     */
    public Paging<ProductVo> pageProduct(ProductParam product) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>();
        if (product.getCategoryId()!=null){
            wrapper.eq(Product::getCategoryId,product.getCategoryId());
        }
        Page<Product> page = new PageInfo<>(product);
        IPage<Product> iPage = this.page(page, wrapper);
        // 最终返回分页对象
        IPage<ProductVo> resultPage = iPage.convert(item -> {
            ProductVo vo = new ProductVo();
            BeanUtil.copyProperties(item, vo);
            return vo;
        });
        return new Paging(iPage);
    }

    public Paging<ProductVo> pageMaProduct(ProductParam productParam) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>();
        if (productParam.getCategoryId()!=null){
            wrapper.and(w->{
                w.eq(Product::getCategoryId,productParam.getCategoryId());
            });
        }
        wrapper.and(w->{
           w.eq(Product::getDelearId,"0").or().apply("FIND_IN_SET("+ StpWxUtils.getLoginId() +",delear_id)");
        });
        wrapper.orderByDesc(BaseEntity::getCreateTime);
        Page<Product> page = new PageInfo<>(productParam);
        IPage<Product> iPage = this.page(page, wrapper);
        List<ProductSku> skuList =new ArrayList<>();
        if (iPage.getTotal()>0){
            skuList.addAll(skuService.list(new LambdaQueryWrapper<ProductSku>()
                    .in(ProductSku::getProductId,iPage.getRecords().stream().map(Product::getId).collect(Collectors.toList()))));
        }
        // 最终返回分页对象
        IPage<ProductVo> resultPage = iPage.convert(item -> {
            ProductVo vo = new ProductVo();
            BeanUtil.copyProperties(item, vo);
            return vo;
        });
        return new Paging(iPage);
    }

    public ProductVo getDetail(Long productId) {
        Product product = getById(productId);
        if (product==null){
            throw new ServiceException("产品信息异常，请联系运营确认此产品是否存在");
        }
        ProductVo vo = new ProductVo();
        BeanUtil.copyProperties(product,vo);
        return vo;
    }
}



